package com.iwith.dongmoon.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.iwith.dongmoon.gsonlist.MemberList;
import com.iwith.dongmoon.util.ApiCode;
import com.iwith.dongmoon.util.ImageDownloader;
import com.iwith.dongmoon.util.ImageDownloader.Mode;
import com.iwith.dongmoon.util.RequestData;
import com.iwith.dongmoon.util.RequestGson;
import com.iwith.dongmoon.util.ResultCode;
import com.iwith.dongmoon.util.ThreadInterface;
import com.iwith.dongmoon.util.ThreadTask;

public class MemberDetailActivity extends Activity implements OnClickListener{

	private RequestGson requestGson = new RequestGson();
	private ImageDownloader mImageDownloader;

	private Button followingBt;
	private TextView prevBt;
	
	private ImageView title;

	private ImageView userPhoto;
	private ImageView[] bizPhoto;
	private Integer[] bizPhotoResource = { R.id.memberdetail_bizimage1, R.id.memberdetail_bizimage2, R.id.memberdetail_bizimage3 };

	private TextView comment;
	private TextView name;
	private TextView graduation;
	private TextView address;
	private TextView phone;
	private TextView bizType;
	private TextView bizName;
	private TextView bizAddress;
	private TextView bizPhone;
	private TextView bizInfo;

	private String userid;
	private String favoriteid;

	private Intent mIntent;

	private Context mContext;

	/** Called when the activity is first created. */

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.memberdetail);
		// TODO Auto-generated method stub
	}

	@Override
	public void onResume(){
		super.onResume();
		ThreadTask.onFinishAll();
		Initialize();
	}
	
	@Override
	public void onPause(){
		super.onPause();
		ThreadTask.onFinishAll();
	}

	private void Initialize() {
		mContext = this;
		
		mIntent = getIntent();
		userid = mIntent.getExtras().getString("userid");
		favoriteid = mIntent.getExtras().getString("favoriteid");

		title = (ImageView) findViewById(R.id.title);
		followingBt = (Button) findViewById(R.id.memberdetail_followingbt);
		prevBt = (TextView) findViewById(R.id.backbtn);
		comment = (TextView) findViewById(R.id.memberdetail_comment);
		name = (TextView) findViewById(R.id.memberdetail_name);
		graduation = (TextView) findViewById(R.id.memberdetail_graduation);
		address = (TextView) findViewById(R.id.memberdetail_address);
		phone = (TextView) findViewById(R.id.memberdetail_phone);
		bizType = (TextView) findViewById(R.id.memberdetail_biztype);
		bizName = (TextView) findViewById(R.id.memberdetail_bizname);
		bizAddress = (TextView) findViewById(R.id.memberdetail_bizaddress);
		bizPhone = (TextView) findViewById(R.id.memberdetail_bizphone);
		bizInfo = (TextView) findViewById(R.id.memberdetail_bizinfo);
		userPhoto = (ImageView) findViewById(R.id.memberdetail_photo);

		bizPhoto = new ImageView[bizPhotoResource.length];
		for (int i = 0; i < bizPhotoResource.length; i++) {
			bizPhoto[i] = (ImageView) findViewById(bizPhotoResource[i]);
			bizPhoto[i].setOnClickListener(this);
		}

		mImageDownloader = new ImageDownloader();
		mImageDownloader.setMode(Mode.CORRECT);

		setData(ApiCode.MEMBERDETAIL);
		//isFavorite(ApiCode.ISFAVORITE);
	
		followingBt.setOnClickListener(this);
		userPhoto.setOnClickListener(this);
		prevBt.setOnClickListener(this);		
		title.setOnClickListener(this);
	}
	private void setData(final String code) {
		ThreadInterface threadInterface = new ThreadInterface() {
			MemberList memberList;
			MemberList memberList2;

			public void onThread() {
				memberList = requestGson.getMemberGsonBySendingAPI(mContext, ApiCode.API_USER + "/" + favoriteid, "GET", "");
				memberList2 = requestGson.getMemberGsonBySendingAPI(mContext, ApiCode.API_ISFAVORITE, "GET", "?userid=" + userid + "&favoriteid=" + favoriteid);
			}

			public void onThreadEnd() {
				if (memberList.getResultCode().equals(ResultCode.SUCCESS) == true) {
					if (memberList.getContent() != null) {
						String[] bizThumbnails = new String[bizPhotoResource.length];
						bizThumbnails[0] = memberList.getContent().getBizThumbnail1();
						bizThumbnails[1] = memberList.getContent().getBizThumbnail2();
						bizThumbnails[2] = memberList.getContent().getBizThumbnail3();

						for (int i = 0; i < bizPhotoResource.length; i++) {
							if (bizThumbnails[i] != null) {
								mImageDownloader.download(bizThumbnails[i], bizPhoto[i]);
							}
						}

						if (memberList.getContent().getThumbnail() != null) {
							mImageDownloader.download(memberList.getContent().getThumbnail(), userPhoto);
						}

						comment.setText(memberList.getContent().getComment());
						name.setText(memberList.getContent().getUsername());
						graduation.setText(memberList.getContent().getGraduation().toString());
						address.setText(memberList.getContent().getAddress());
						phone.setText(memberList.getContent().getPhone());
						bizType.setText(memberList.getContent().getBizType());
						bizName.setText(memberList.getContent().getBizName());
						bizAddress.setText(memberList.getContent().getBizAddress());
						bizPhone.setText(memberList.getContent().getBizPhone());
						bizInfo.setText(memberList.getContent().getBizInfo());
					}
				} else {
					requestGson.alertToast(mContext, code + favoriteid + " 데이터 로딩 실패");
				}
				
				if (memberList2.getResultCode().equals(ResultCode.SUCCESS) == true) {
					followingBt.setText("관심 동문에서 지우기");
					followingBt.setTag(ApiCode.DELETEFAVORITE);
				} else if (memberList2.getResultCode().equals(ResultCode.SUCCESS) == false) {
					followingBt.setText("관심 동문에 추가하기");
					followingBt.setTag(ApiCode.ADDFAVORITE);
				} 
			}
		};
		ThreadTask task = new ThreadTask((Activity) mContext, threadInterface);
		task.execute();
	}

	private void setFavorite(final String code) {
		MemberList memberList;

		String api = null;
		if (code.equals(ApiCode.DELETEFAVORITE) == true) {
			api = ApiCode.API_DELETEFAVORITE;
		} else if (code.equals(ApiCode.ADDFAVORITE) == true) {
			api = ApiCode.API_ADDFAVORITE;
		}

		Gson gson = new Gson();
		RequestData data = new RequestData(mContext, api, "POST", "");
		data.addParameter("userid", userid);
		data.addParameter("favoriteid", favoriteid);
		String sresult = data.getDataPost();
		memberList = gson.fromJson(sresult, MemberList.class);

		if (memberList.getResultCode().equals(ResultCode.SUCCESS) == true) {
			if (code.equals(ApiCode.DELETEFAVORITE) == true) {
				followingBt.setText("관심 동문에 추가하기");
				followingBt.setTag(ApiCode.ADDFAVORITE);
			} else if (code.equals(ApiCode.ADDFAVORITE) == true) {
				followingBt.setText("관심 동문에서 지우기");
				followingBt.setTag(ApiCode.DELETEFAVORITE);
			}
		} else if (memberList.getResultCode().equals(ResultCode.SUCCESS) == false) {
			if (code.equals(ApiCode.DELETEFAVORITE) == true) {
				requestGson.alertToast(mContext, "관심동문 지우기 실패");
			} else if (code.equals(ApiCode.ADDFAVORITE) == true) {
				requestGson.alertToast(mContext, "관심동문 추가하기 실패");
			}
		}
	}

	

	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v.getId()==prevBt.getId()){
			finish();
		} else if(v.getId()==title.getId()){
			Intent intent=new Intent(mContext, MainIconView.class);
			intent.putExtra("flag", true);
			startActivity(intent);
		} else if(v.getId()==userPhoto.getId()){
			Intent intent = new Intent(mContext, PhotosActivity.class);
			intent.putExtra("userid", userid);
			intent.putExtra("code", ApiCode.USERPHOTO);
			startActivity(intent);
		} else if(v.getId()==followingBt.getId()){
			String tag=(String)v.getTag();
			setFavorite(tag);
		} else {
			for (int i = 0; i < bizPhotoResource.length; i++) {
				if(v.getId()==bizPhoto[i].getId()){
					Intent intent = new Intent(mContext, PhotosActivity.class);
					intent.putExtra("userid", userid);
					intent.putExtra("code",	ApiCode.BIZPHOTO);
					startActivity(intent);
				}
			}
		}
	}
}